package com.example.service.impl;

import com.example.controller.utils.A;
import com.example.controller.utils.CodeUtils;
import com.example.domain.SMSCode;
import com.example.service.SMSCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class SMSCodeServiceImpl implements SMSCodeService {

    @Autowired
    private CodeUtils codeUtils;

    @Override
    @CachePut(value = "smsCode", key = "#tele")
    public String sendCodeToSMS(String tele) {
        if (tele.length() < 11) {
            return "请输入正确的手机号码";
        } else {
            String code = codeUtils.generator(tele);
            return code;
        }
    }

    @Override
    public A checkCode(SMSCode smsCode) {
        //取出内存中的验证码与传递过来的验证码比对
        String code = smsCode.getCode();
        String cacheCode = codeUtils.get(smsCode.getTele());
        if (code.equals(cacheCode)) {
            return new A(true, "验证成功");
        } else {
            return new A(false, "验证失败");
        }
    }
}